package myproject.computer;

/**
 * @author shkstart
 * @creat 2022-04-16:35
 */
public class Disk {
    private int capacity;//磁盘大小
    private String diskName;//磁盘名
    private SoftWare[] softWares = new SoftWare[10];
    private int countOfWare;

    public Disk() {
    }

    public Disk(int capacity, String diskName) {
        this.capacity = capacity;
        this.diskName = diskName;
    }

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    public String getDiskName() {
        return diskName;
    }

    public void setDiskName(String diskName) {
        this.diskName = diskName;
    }

    public SoftWare[] getSoftWares() {
        return softWares;
    }

    public void setSoftWares(SoftWare[] softWares) {
        this.softWares = softWares;
    }

    public int getCountOfWare() {
        return countOfWare;
    }

    public void setCountOfWare(int countOfWare) {
        this.countOfWare = countOfWare;
    }

    /**
     * 安装软件
     *
     * @param softWare
     */
    public void addSoftware(SoftWare softWare) {
        //软件对象数组扩容
        if (countOfWare > softWares.length) {
            SoftWare[] newSoftWares = new SoftWare[softWares.length * 2];
            for (int i = 0; i < countOfWare; i++) {
                newSoftWares[i] = softWares[i];
            }
            softWares = newSoftWares;
        }
        softWares[countOfWare++] = softWare;
        System.out.println(softWare.getWareName() + "安装成功");
    }

    /**
     * 读取磁盘安装的软件
     */
    public void readDisk() {

        if (countOfWare == 0) {
            System.out.println(diskName + "并未安装软件");
        }

        for (int i = 0; i < countOfWare; i++) {
            System.out.println(softWares[i]);
        }
    }

    /**
     * @return 返回软件大小
     */
    public int wareOfTotalSize() {
        int total = 0;
        for (int i = 0; i < countOfWare; i++) {
            total += softWares[i].getSize();
        }
        return total;
    }



    /**
     * 利用磁盘里的下载软件去下载其他软件
     * @param downWareName
     * @param wareName
     * @param wareSize
     * @return true:安装成功，false:笼统为安装失败
     */
    public boolean down(String downWareName, String wareName, int wareSize) {
        if (countOfWare == 0) {
            return false;
        }
        for (int i = 0; i < countOfWare; i++) {
            if (downWareName.equals(softWares[i].getWareName())) {
                //软件启动
                softWares[i].run();
                if (softWares[i] instanceof DownloadSoftWare) {
                    DownloadSoftWare d = (DownloadSoftWare) softWares[i];
                    d.xiazai(new SoftWare(wareName, wareSize));
                    return true;

                }
            }
        }
        System.out.println("没有此下载软件");
        return false;
    }
}
